<template>
  <div class="page-container">
    <div class="container">
      <h2>邮箱重置密码</h2>
      <!-- 邮箱输入框 -->
      <div class="input-group">
        <input type="email" v-model="email" placeholder="请输入邮箱" required />
      </div>
      <!-- 验证码输入框和发送验证码按钮 -->
      <div class="input-group">
        <input type="text" v-model="verificationCode" placeholder="请输入验证码" required />
        <button 
          class="btn-complete" 
          @click="sendVerificationCode" 
          :disabled="!isValidEmail || isSendingCode"
        >
          {{ isSendingCode ? countdown + 's 后重试' : '发送验证码' }}
        </button>
      </div>
      <button class="btn-complete" @click="verifyCode" :disabled="!isFormValid">确认</button>
      <div class="alternative-method">
        <router-link to="/phone-reset-password" class="forgot-link">使用手机号找回密码</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      email: '',
      verificationCode: '',
      isSendingCode: false,
      countdown: 60
    }
  },
  computed: {
    isValidEmail() {
      return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email)
    },
    isFormValid() {
      return this.isValidEmail && this.verificationCode
    }
  },
  methods: {
    async sendVerificationCode() {
      if (!this.isValidEmail) {
        this.$message.error('请输入有效的邮箱地址')
        return
      }

      this.isSendingCode = true
      this.countdown = 60

      try {
        const response = await axios.post('/api/users/send-code/', {
          email: this.email,
          scene: 'reset_pwd'
        })
        
        if (response.data.status === 'success') {
          this.$message.success('验证码已发送')
        } else {
          this.$message.error(response.data.message || '验证码发送失败')
        }
      } catch (error) {
        console.error('请求出错:', error)
        // 更详细的错误处理
        if (error.response?.data?.message) {
          this.$message.error(error.response.data.message)
        } else {
          this.$message.error('网络错误，请重试')
        }
      } finally {
        const timer = setInterval(() => {
          if (this.countdown <= 0) {
            clearInterval(timer)
            this.isSendingCode = false
          } else {
            this.countdown--
          }
        }, 1000)
      }
    },

    async verifyCode() {
      if (!this.isFormValid) {
        this.$message.error('请填写完整信息')
        return
      }

      try {
        const response = await axios.post('/api/users/check-code/', {
          email: this.email,
          code: this.verificationCode,
          scene:'reset_pwd'
        })

        if (response.data.status === 'success') {
          // 保存token和邮箱到cookie
          document.cookie = `reset_token=${response.data.token}; path=/`;
          document.cookie = `reset_email=${this.email}; path=/`;
          this.$router.push('/password')
        } else {
          this.$message.error(response.data.message || '验证码错误')
        }
      } catch (error) {
        console.error('请求出错:', error)
        if (error.response?.data?.message) {
          this.$message.error(error.response.data.message)
        } else {
          this.$message.error('网络错误，请重试')
        }
      }
    }
  }
}
</script>

<style scoped>
.page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.container {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 300px;
  text-align: center;
}

.alternative-method {
  margin-top: 15px;
}

.forgot-link {
  color: #007bff;
  text-decoration: none;
  font-size: 14px;
}

.forgot-link:hover {
  text-decoration: underline;
}

h2 {
  margin-bottom: 20px;
}

.input-group {
  margin-bottom: 15px;
  display: flex;
  gap: 10px;
}

.input-group input {
  flex: 1;
  padding: 10px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.btn-complete {
  background-color: #007bff;
  color: white;
  padding: 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  white-space: nowrap;
  width: 100%;
}

.btn-complete:hover {
  background-color: #0056b3;
}

.btn-complete:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}
</style>